package com.uyayo.framework.common.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * DES加密解密类.
 * Created by Administrator on 2018/6/5.
 * @version 1.0
 */
public class DesUtils {
    /**
     * 加密、解密key.
     */
    private static final String PASSWORD_CRYPT_KEY = "best8888";

    /**
     * 加密
     */

    private final static String ALGORITHM = "DES";

    public static void main(String[] args) throws Exception {

        String md5Password = "best";
        System.out.println(md5Password.length());
        String str = DesUtils.encrypt(md5Password);
        System.out.println("str: " + str);
        str = DesUtils.decrypt("15819D6049B08E9DE33ED4B1FF500F93504C2D569303FDC665099092507E4917E2DA57DAED1ECC9CD454209F24A57BC1C546E621453B8C1561ADEE4ACF9828C53AFADB01F551098E803FA09CB0B5032E195807E2713F11771358A4666FB85FFA04B484A20405AB0CEE1772AE88063EAE4DF76AF69D8DC434F35A10F18B79064AA6EB1D8083E702042AD2F4F16AB581B5E23D4396E5A795938314D678B8B120F619AA48DC9B1D20F4FF44D692F842BF76F3ADA26E0FD44B298C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC6017BB3C7A5183D7110617DB64C807727E08C89E2529691CE8C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC63AF2C1D658DB743EEE1772AE88063EAEBDEF488CFAE3F37383D321065AEDE5E303DA743A048365833168A66DFD5FFBAF5414366D68476273D65DE7E5FAB45BEF85195BEB11D6C6C6326850E9AFDE3D9AD0E65294C0632110561B9A40B223610ABDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974CD2103F79BAD26BFAE5F48C41A8DFBAA1E0AB3214BD80771C0F35A1B4D0F912B896FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F549341A7472ADE65CB53B4112BB5083E61C21DD1ADAD6192A284AC4B6E353B2AA878102414813F3750A9BA5F5DEC94E6263DCC8F501B3417AE7E160F382814E4A7F875367CC043974CF9706951A35ACD8B5E32EAD3B046DD7412803D2F68A480407ABAD899908F7436676595296FF74E5F9B08EA4FC4DE02436677C8015D691480F9A364E26DDEFD21C68D5F70977119390FE3DA15A5E97F05B0834A30A98B29826E5D592B389C3F0DC5774BB3CC83BEE4313C0B0307ADB0AA5F6B41767E1AD566EA5507DB7052AF03ECA71132418DCEB6A34D98474663B06A6BBBBA3BF775A4B8BDC2BFCD8ABA75DEFFB2700201198F4856E64B3F47E032F7FA30F1D8E11F7FFEB1DC39135A8860809341A7472ADE65CB8C54286719ECF174EE1772AE88063EAE06CABA2B9E62F2C8848B5FCC19CDC166CA02D3686DFA0935732044F4EAB387FA3E9EF7E2872F94DD7C8B1552035BE9915E3DA58C75BD7623177A4C1BFDC77FB9EF5CF8CE9FB3B021C38CF89A5549F5DA45922BDCE7AAF0C000C0B1F7E02B93DEAB50334ADD05166D40652A4E4ADC9BB5C5774BB3CC83BEE4313C0B0307ADB0AA5F6B41767E1AD56602C38E473F4E0F8DEE1772AE88063EAE0701B2322183F3A9728C5789EF6092DAA6EB1D8083E70204C09B50E357B300BDE23D4396E5A795930513C30300B6FDC36A7850FBC62125CD78CCD5A011792F24FE3F46820EDC87B8ED47A783C05FF1B07FBDC63D3240DB30D158E2A68104CE32F899C8BCD5BD5F549992A5CF8CC5225FDB6A9234197D8FD81FA0F908B31924CACE1489B397080E2B21FE95BD649563AF4A847BB77A998BFD1B2961592AD6E096D4DEBD34BA7A2BBD30D013041E45583F62CC2D1F64AE9EFC504C2D569303FDC603B1790FA6F98FAF287B7C77D759611A1AA0F82F532E63AE61196972C24757209987F1B975379FC969C7222EA76DE97F195807E2713F1177DB6905C3CE0F8C853A9F47415139DE67DA7E0914E36F2EE577C723E74AF68006BDC2BFCD8ABA75DE7E160F382814E4A7F875367CC043974C52214F433CDE303D6FE5031DB15B54A4F7CF729BDBBDF8D75E74821C5F458D6F676595296FF74E5F9B08EA4FC4DE02430BDAB2C3B52573DE2E2FA03122A0207919210037A166D22CFDF5332DADE0A9CFADB8F442A91117B718146CA4515511EC47373926C83D1B3CF99D51D0F7804830797D91344E2ECE8830FC655C9EC86A7E30D013041E45583F62CC2D1F64AE9EFC504C2D569303FDC6717528955BE3DCCD2D5A11A76AC9E4FD2E18F7B20280F113ADEB0212FA59C8A142EFF5461A4ED1BC619AA910FF3579C469C7222EA76DE97F195807E2713F1177272BB508FD991BF2DC3E06DF0EDF499E38FE09B9FC10E67C8DEC22D58E8FB0E0676595296FF74E5F9B08EA4FC4DE02436677C8015D6914802E8805E9D0020B0D3F33D138963FFE3581AF34C2DEF23FA921D1B65600AA60B796FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F549341A7472ADE65CB85432EF85ADEE2310FE3DA15A5E97F0578647D5DB047DB9CCBCB57946A1B659AC5774BB3CC83BEE4313C0B0307ADB0AA5F6B41767E1AD566AB5155EA34F5197B7A51C0D883D97205C06D745D9ABB41398047302590EDC82D9987F1B975379FC969C7222EA76DE97F195807E2713F1177D49E09E9968DF850B64A3FEE4AB6A9137FBDB5BBA3F6AC3E3FC0CD870E2035CFE865464F7F3904CE7CD3E88532F6584083CF9B6D7D715787D65DE7E5FAB45BEF0FFA73A42C9BCEFCC10F6B0CB9B3D2ECBCEE8A99ABEAAC4CEE05E0C5F6FB878B9987F1B975379FC969C7222EA76DE97F195807E2713F117786C084CC3C60F161625633C55EC99219F4D98114E6646A47FD401737835EC8C470C11C75F5C94F0586841327391B1912803FA09CB0B5032E195807E2713F11774EE3929703A4AAD64469F79F63AAC3EFE0AB3214BD80771C6769F21F9DDC944696FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F549341A7472ADE65CB411E206BE8A2CBB7040406B9D165D7FF142E006634B55C7797A8F88B6CAFE0297CD3E88532F6584083CF9B6D7D715787D65DE7E5FAB45BEF816E844EE1E2C109287B7C77D759611AAADEC445F8FE10814005F1DB714126459987F1B975379FC969C7222EA76DE97F195807E2713F11774A8C0F949BDE66B30474C8C556F3113F15FDEDD0E232896FEB6E5A9301827ECDBDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974C23AA2AA0A3DD0534E7939AB4F74A70A80D40650AA08EBDE84055159F8F40C2B4B8081B7491506DC8A6EB1D8083E70204C09B50E357B300BDE23D4396E5A79593DA245D02A1503BBDB21CBA2FB00D07BA3D7361B4B157DD5C2908F0965246C1C17CD3E88532F6584083CF9B6D7D715787D65DE7E5FAB45BEFC07802D54DD2C8A7549BBF19E85AE98EE7493EED9DBC542179561EC8D902383E9987F1B975379FC969C7222EA76DE97FFCD9AF967BD52BDCB69557FA1C6C2657DB6A9234197D8FD85438B6DDFB7C08999883E90D523976220D40650AA08EBDE861C1B9B6A94D603E049A63D35E40F66011C072C756EBCF8D313C0B0307ADB0AA32C8583C1C85F2729AC04FF78EC55D81E9E12DF30760BEE20035DE43D091B78A9022EFB44B2AC854948C7020D9A46F7A70CB598075652099BDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974CA418F281F5368B50AB37BBDC9C8AEE147904A1DA5850E00CFA25FF7AD96CBD0F915245FA1F49F1B1A94C7E10C026633209C17E7C1E96395D7B655280164499A9208834F675C3962580CB915E9137C8B2504C2D569303FDC65946A46C110AF5D8481C92353F05B600E150A9B47F2B3CFC0F99B557EF7492439987F1B975379FC969C7222EA76DE97F195807E2713F11774870A84082719FB911D97BB194AF56E6F5FAD189FEE424AA0047317E68DDBC987E98B47F5656DC657737D40BFC9686E46D54592C3BB45983F899C8BCD5BD5F549341A7472ADE65CB282A38678A53FC5AD21D2E34203F0C12A6DB884E94F8CDC3FC1F53D22E53AB84FC7858BEA28A63F53819783A48CAE2602AD2F4F16AB581B5E23D4396E5A795939E8CD05AA733F73CF394925A376B13D92BFC3B196DC942E8B402A6EADC51DC5F7CD3E88532F6584083CF9B6D7D715787D65DE7E5FAB45BEFA1B309CE5767DA6692B6BAB65F25AA72C403973D80D4BB6BE7918E2CDE00F06FBDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974CDE01812B6F4D46B14C77BF801C1D3C4C06C0A980EB190B42C575A19E330C01C7E0B2A65211AD053DC5CAEDE20C9DF52A700036F3D2B73DE96677C8015D691480CF665F25ED9EA7BE2A3B156FDD5DB1A7FB265EBD9C31F16D2BE4E0BD698F704E8C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC64AD4F6EAD2B10436467013BCCCF66D13899D72EE2A246AB396FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F549341A7472ADE65CB4B2DE053DAB1AF2AF22022BF74B37FEE65FB0F8142E7F604BC281B310179B607977D86D25B9A93FDDA9F13B302C01F42A87907F2FD4DFB28F875367CC043974CC521D4CB6C09EBECA4300A6BA1665D5C783940EC84833DDB4F8292DD71C53713676595296FF74E5F9B08EA4FC4DE0243EBE80B10EAC2CA530199E0BF31DF63597F9EFE35928929A3C34E8FD386429379F928552642AFA42D17F0AC507C106A0F7AA5589ED474144DA8B83AF086FC2C7E3A4C7DD10E566E10D158E2A68104CE32F899C8BCD5BD5F549341A7472ADE65CB6CFF94DC43C0ABE4183C36154E1889349394EB83746DE095FC1F53D22E53AB84C5774BB3CC83BEE4786279DB9E652FC4\n" +
                "15819D6049B08E9DE33ED4B1FF500F93504C2D569303FDC6420DE5E640C272B4B4AAA341CB44728598402B4F5AFDE7FC96FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F54252403B67FDA3A83FB1981F9E4E376E69341A7472ADE65CB066BB987C11620CC7A599F16B77C4827FE8305155D56EC7820E71629C334399D668C856DB562576268D1184F228903FF7EBB65261078118E195807E2713F1177D273720A1FB3A054FD1F8CC108BEF250AFDCFD10A82EDFB9B9A5CB937793ABCF915245FA1F49F1B1FF15E93EB95A4A8B7EBB65261078118E195807E2713F11776B3A131592220DD790AA77C38071D71BB1B82410D7E51ECF0039CA0953F989FDBBB073F8AF2745FCA6EB1D8083E70204C09B50E357B300BDE23D4396E5A7959330BD387461992F14CFF7FBCD69BA09DE7C19B7C3B19A77F5BDBA88FB8A2A5B19112B4C4A29D25616AD754E6B3C2433DE700036F3D2B73DE9193078E4E46B9DB05FD3A9976F79CFC5177A4C1BFDC77FB98F88A30DB695289AE1BEC64D545F9BE049FE7A204184211E1E930C17AA9B8D0CD2AF9DFB40F919C7B1FB3BAB8886B654198720E2A9E6B676B77BCEBF758C2E651CC46E901B8AC2DA177A4C1BFDC77FB9BB8D05D58A9153942DDEE41DF9979C9215D9E89B4AA71C635150F4EE3AAA70F096FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F549341A7472ADE65CBCB670C2578BEF2AF183C36154E188934625B015283561B3B528C098CF19E57DEC5774BB3CC83BEE4313C0B0307ADB0AA5F6B41767E1AD5664D981FC2C615E293EE1772AE88063EAEB069C21806EAFBBEC5D85B73EC45C181A6EB1D8083E70204C09B50E357B300BDE23D4396E5A7959325F283AE7C8FAB84E2DA57DAED1ECC9C90670AA6C2625AFCB930A2008586D80A7CD3E88532F6584083CF9B6D7D7157876CF165AF142F28C763790EE4DC2D0409177A4C1BFDC77FB96CAF1FC70E524EFF8B61528C326D1B178ABF0A303F1435E29A5ED7DAC186CBB69987F1B975379FC969C7222EA76DE97F195807E2713F11777F5C59AB3F2E133EA4C752F9EC20AC6A0D40650AA08EBDE8C503F08295B375ABCBCB57946A1B659A11C072C756EBCF8D313C0B0307ADB0AA5C4F0635402D913BD5B9D43F8846BF9ADB6A9234197D8FD89E411ECF64F20B104FB446DA31318C90EA867838606BBB491FB7C8C5E064EB30BDC2BFCD8ABA75DE7E160F382814E4A7F875367CC043974CF8452C179D7126AC27090CA867620A2363D9088CE00EB43E4ABBD8EA747DECFC676595296FF74E5F9B08EA4FC4DE02436677C8015D6914801E4888182DAA8AE2EA9F945BABF6052A87FF57AA49DB4A9892E67ADCE7C97C181E7C7CFC56A238A8C95A441A10518303FFB2700201198F48F875367CC043974C980D32D458C835C14158B02C4C411BA4DEBF028BC71DD72F21FBE56D12EB13BA96FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F549341A7472ADE65CB37B903522A371A878B61528C326D1B170063F3D97D8F1A8BC09450A398B7276F42CC383ACCEB3CC59776AAABDBAA80647E160F382814E4A7F875367CC043974C796D722B4E3107F21F07546DCE86913932E9EF8DB851AB5DF17A9E24B9B563E496FDF98B71A4D8B3C13351F5A28AEE40F899C8BCD5BD5F540626ADB1F2F2F662B3EA1A090873C19B9341A7472ADE65CB7F6E125164D76CB901C8BFE9FA72A042811EBE73745CC6BBB40C405381A6EBC19987F1B975379FC969C7222EA76DE97F195807E2713F117780BBB4A502F3F1852157E6C0C93397D2A5269F7C3CD4B7232C3E053EB3D003B0BDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974C803DCF24CDFB7F6459F42666A81CE677B5E08CC08E60282CE4A000793A4529D38C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC6C5C63DF4486385A030C57A2FE50B53F2153D2489A592451E676595296FF74E5F9B08EA4FC4DE02436677C8015D6914807A21D7CCFE0D98F5EDCAC23E93A5341830C57A2FE50B53F28519AB8F506838D78C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC63D867F4E244B6D57033C9BC6CEB4E34480B2978709F2E26B8C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC64AA370A2F78B5399033C9BC6CEB4E344987AEE6A1FDF52118C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC6030DEC2D770AE777033C9BC6CEB4E344DFAFB82548CE58EA8C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC6CDC0EF0E91C125B8033C9BC6CEB4E34419AB8A17F2601DCA8C0EC02573D63E5009C17E7C1E96395D62CC2D1F64AE9EFC504C2D569303FDC6DD1397211EA73D1BEE1772AE88063EAE200C45BEF0CA1F8A35908A555E273E4AA6EB1D8083E70204C09B50E357B300BDE23D4396E5A79593A051EA0190F9E9AA475C4DE4230DA7EBBB5F0B438A3438E27EFF5075B7FDD4AD7CD3E88532F6584083CF9B6D7D715787D65DE7E5FAB45BEF2B0C149C170351E90E632C48F1B2392EF9ACBC2DD775B40F0A7CF60F8D8BEA8A9987F1B975379FC969C7222EA76DE97F195807E2713F1177AAB904F45B9ED2EEDEE56157385DBDB3183C36154E188934077C08B293433588049A63D35E40F660C5774BB3CC83BEE4313C0B0307ADB0AA5F6B41767E1AD5669E8D57A561ABF77CD9B566422D5AC8BAC0B0A834B1905C08C4A202A49D407486BDC2BFCD8ABA75DE7E160F382814E4A7F875367CC043974CCDE95DD8A21D7B78CD38BD530C529DC4EE1772AE88063EAEA996470322A82F5912685998829CE9F3664E8169CA1F237CAEBC2669A3912B513E9EF7E2872F94DDD65DE7E5FAB45BEF868B190564441E744DC40F5ADD57E671A9C839D20E449233E8B32AE35541F2A29987F1B975379FC969C7222EA76DE97F195807E2713F11770BA61076917A221B95FA9A7DB751A7ED70833776CD14AEAA3B5580F633E161F9BDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974C01FE7C8B50B1AA73D65C76AE53420F0D183C36154E1889341F1A116F146F1921FC1F53D22E53AB8411C072C756EBCF8D313C0B0307ADB0AA5F6B41767E1AD5667A4DBCBEAE95A9DDEE1772AE88063EAE6E1749B368FD89E71526F025C78A52A0A6EB1D8083E70204C09B50E357B300BDE23D4396E5A79593D79356FEC431F6E884457F69B200DBCE6087C192030E68ECCCEE8A626F1DA19EBDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974CDE01812B6F4D46B1CB68A238EC36908F0D40650AA08EBDE89ADC5C0EF97FE8F06AA3A417A3A2CAC911C072C756EBCF8D313C0B0307ADB0AA5F6B41767E1AD566BA56B39078E931D4EE1772AE88063EAE4BBDB4246F9B0DDCF452516393EFF603A6EB1D8083E70204C09B50E357B300BDE23D4396E5A79593081385C2066650E6E18A696629823B6723AC9213F6A22B2908D857968F4FB7107CD3E88532F6584083CF9B6D7D715787D65DE7E5FAB45BEF95319472042D4AD31E6656D42F0B139AB36CEBFFB3A4A122793E0DAFB300038ABDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974C9551FBC4C7F4F5E07E487DCD822F660F68E31AB2F06E9AA3A0153CBCABE803AA8C0EC02573D63E5030D013041E45583F62CC2D1F64AE9EFC504C2D569303FDC6A558B26CA6AFF2B6EE1772AE88063EAE15295EDF825036C2492529CEBC9508DEA6EB1D8083E70204C09B50E357B300BDE23D4396E5A79593E26E4BA5CD50A71207C28B983D29278512C00B07650FB0A91526F025C78A52A0A6EB1D8083E70204C09B50E357B300BDE23D4396E5A79593071E45CE28553E2CCCD6FCA95F80DE0F5DB1B9A16D54B706B794E8EDDB2A3F947FE91BF472DD43E4A71B3031ED7CEA9D7EBB65261078118E195807E2713F1177D8A85F2B50E94369DF3D09658E0F07B42024FFE3EA6DDCAC4E0CFCFED6F9F85FBDC2BFCD8ABA75DEFFB2700201198F48F875367CC043974C925B4A9EB130DD89660CCC4C5425C382EE1772AE88063EAE8C8D4CB30661498C56377056A3997AFEC0B51C82BBA5F6A9BED6FAE5E37D8F7083CF9B6D7D71578717069F7853D1B84CFBE01D42374A63CFDB6A9234197D8FD8FD0AAEEC3F15ABF14FB446DA31318C90580C853C71DAB3F46C1B871B4072153DBDC2BFCD8ABA75DE7E160F382814E4A7451926C42A7B99A33B71DD220489CFED8331215609F079B3991EF134C50B844140E6B6A8FD0B6D303E229F83B777161C291476E2EFF63E358F5083BF9A57CA8D87CC102AFB7A9A1FFF3AFD2045F1DF45305FBD511DC8728F");
        System.out.println("str: " + str);
    }


    /**
     * 对数据进行DES加密.
     *
     * @param data 待进行DES加密的数据
     * @return 返回经过DES加密后的数据
     * @throws Exception 全局异常
     * @author uyayo
     * Creation date: 2007-7-31 - 下午12:06:24
     */

    public static String decrypt(String data) throws Exception {
        return new String(decrypt(hex2byte(data.getBytes()), PASSWORD_CRYPT_KEY.getBytes()));
    }

    /**
     * 对用DES加密过的数据进行解密.
     *
     * @param data DES加密数据
     * @return 返回解密后的数据
     * @throws Exception
     * @author uyayo
     *
     * Creation date: 2007-7-31 - 下午12:07:54
     */

    public static String encrypt(String data) throws Exception {

        return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY.getBytes()));

    }


    /**
     * 用指定的key对数据进行DES加密.
     *
     * @param data 待加密的数据
     * @param key  DES加密的key
     * @return 返回DES加密后的数据
     * @throws Exception
     * @author uyayo
     *
     * Creation date: 2007-7-31 - 下午12:09:03
     */

    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {

        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密匙数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密匙工厂，然后用它把DESKeySpec转换成

        // 一个SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);

        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        // 用密匙初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        // 现在，获取数据并加密

        // 正式执行加密操作
        return cipher.doFinal(data);

    }

    /**
     * 用指定的key对数据进行DES解密.
     *
     * @param data 待解密的数据
     * @param key  DES解密的key
     * @return 返回DES解密后的数据
     * @throws Exception
     * @author uyayo
     *
     * Creation date: 2007-7-31 - 下午12:10:34
     */

    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {

        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密匙数据创建一个DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密匙工厂，然后用它把DESKeySpec对象转换成

        // 一个SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);

        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        // 现在，获取数据并解密
        // 正式执行解密操作
        return cipher.doFinal(data);
    }

    private static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    private static String byte2hex(byte[] b) {
        StringBuilder stringBuilder = new StringBuilder();
        String stomp;
        for (byte aB : b) {
            stomp = (Integer.toHexString(aB & 0XFF));
            if (stomp.length() == 1)
                stringBuilder.append("0").append(stomp);
            else
                stringBuilder.append(stomp);
        }
        return stringBuilder.toString().toUpperCase();
    }
}
